package chapter02;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Training09 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double[] score = null;

        // 任务一：显示提示信息并等待用户输入人数
        System.out.print("请输入班级人数:");
        int numStudents = 0;
        try {
            numStudents = scanner.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("输入错误，请输入一个整数。");
            return; // 如果输入错误，结束程序
        }

        // 初始化成绩数组
        score = new double[numStudents];

        // 任务二：循环输入用户成绩并记录到score数组中
        for (int i = 0; i < numStudents; i++) {
            System.out.print("请输入学号" + (i + 1) + "的学生的成绩:");
            try {
                score[i] = scanner.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请输入一个数字。");
                i--; // 输入错误，重试当前学生的成绩输入
            }
        }

        // 计算总成绩和平均成绩
        double sum = 0;
        for (double s : score) {
            sum += s;
        }
        double average = sum / numStudents;

        // 任务三：输出平均成绩和统计各分数段人数
        System.out.println("全班平均成绩为: " + average);
        int above80 = 0, between70And80 = 0, between60And70 = 0, below60 = 0;
        for (double s : score) {
            if (s > 80) {
                above80++;
            } else if (s >= 70) {
                between70And80++;
            } else if (s >= 60) {
                between60And70++;
            } else {
                below60++;
            }
        }
        System.out.println("成绩大于80分: " + above80);
        System.out.println("成绩大于70分: " + (above80 + between70And80));
        System.out.println("成绩大于60分: " + (above80 + between70And80 + between60And70));
        System.out.println("成绩不及格人数: " + below60);

        // 关闭scanner
        scanner.close();
    }
}